package com.yuyou.fn.controller;

import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import com.yuyou.fn.common.service.IQiniuService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by wens on 16/7/8.
 */

@Controller
public class QiniuController {

    private final  static Logger log = LoggerFactory.getLogger(QiniuController.class) ;

    @Resource
    private IQiniuService qiniuService ;


    @RequestMapping("/api/qiniu/upload")
    @ResponseBody
    public ReturnResult<String> upload(MultipartFile file,@RequestParam(value = "type",defaultValue = "picture") String type) throws Exception{

        //写入本地磁盘
        InputStream is = file.getInputStream();
        String url =qiniuService.upload(is,type);
        is.close();
        return new ReturnResult(true,"",url);
    }


    @RequestMapping(value = { "/api/qiniu/upload/token"})
    @ResponseBody
    public Map<String, String> uploadToken( @RequestParam(value = "opts", required = false) String opts) {

        StringMap stringMap = new StringMap().put("returnBody", "{\"original\":$(fname),\"url\":${key},\"state\":\"SUCCESS\",\"size\":${fsize},\"type\":${mimeType},\"avinfo\":${avinfo},\"persistentId\":${persistentId} }");
        if (StringUtils.isNoneEmpty(opts)) {
            stringMap.put("persistentPipeline", "yuyou1");
            stringMap.put("persistentOps", opts);
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("uptoken", qiniuService.getAuth().uploadToken(qiniuService.getBucket(), null, 1800, stringMap));
        return map;
    }

    @RequestMapping("/api/qiniu/upload/video/token")
    @ResponseBody
    public Map<String, String> uploadVideoToken() {
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        StringMap stringMap = new StringMap().put("returnBody", "{\"original\":$(fname),\"url\":${key},\"state\":\"SUCCESS\",\"size\":${fsize},\"type\":${mimeType},\"avinfo\":${avinfo},\"persistentId\":${persistentId} }");
        stringMap.put("saveKey", key);
        stringMap.put("persistentPipeline", "yuyou1");
        stringMap.put("persistentOps", "vframe/png/offset/10/w/480/h/360|saveas/" + UrlSafeBase64.encodeToString(qiniuService.getBucket() + ":" + key + "-thumbnail-2") + ";" +
                "vframe/png/offset/30/w/480/h/360|saveas/" + UrlSafeBase64.encodeToString(qiniuService.getBucket() + ":" + key + "-thumbnail-30") + ";" +
                "avthumb/mp4/vcodec/libx264|saveas/" + UrlSafeBase64.encodeToString(qiniuService.getBucket() + ":" + key ) + ";" +
                "avthumb/m3u8/vcodec/libx264|saveas/" + UrlSafeBase64.encodeToString(qiniuService.getBucket() + ":" + key + ".m3u8" )
                );
        Map<String, String> map = new HashMap<String, String>();
        map.put("uptoken", qiniuService.getAuth().uploadToken(qiniuService.getBucket(), null, 1800, stringMap));
        return map;
    }


}
